package com.gitee.huxing.algorithmic.daily;

/**
 * @Description: 乘积小于K的连续子数组
 * 给你一个整数数组 nums 和一个整数 k ，请你返回子数组内所有元素的乘积严格小于 k 的连续子数组的数目。
 * @ProjectName: algorithmic-pactice
 * @Package: NumSubarrayProductLessThanK
 * @ClassName: NumSubarrayProductLessThanK
 * @Author: huxingmale
 * @DateTime: 2022-05-20 9:09
 */
public class NumSubarrayProductLessThanK {

    public static void main(String[] args) {
        NumSubarrayProductLessThanK thanK = new NumSubarrayProductLessThanK();
        int[] nums = {10, 5, 2, 6};
        int k = 100;
        System.out.println(thanK.numSubarrayProductLessThanK(nums, k));
    }

    /**
     * @Description:  乘积小于K的连续子数组
     * @Author: huxingmale
     * @param nums    数组
     * @param k       乘积
     * @return int
     * @Date: 2022/5/20 9:11
     **/
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        if (k < 1){
            return 0;
        }
        int count = 0, left = 0, n = nums.length;
        while (left < n){
            int total = 1;
            for (int right = left; right < n; right++){
                total *= nums[right];
                if (total < k){
                    count++;
                } else {
                    break;
                }
            }
            left++;
        }
        return count;
    }
}
